import json
import requests


class MySession(requests.Session):
    def request(self, *args, **kwargs):
        kwargs.setdefault('timeout', 5)
        return super(MySession, self).request(*args, **kwargs)

def lis_generator(pid, start_time, end_time, reportid_required=None):

    headers = {
                "User-Agent":
                    "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"
            }
    # 获取授权
    # url_auth = "http://10.68.60.145:8001/Clinician/FunsCL/ViewReport"
    url_auth = "http://10.68.60.145:8001/Clinician/FunsCL/ViewReport/user=5613/patno=%s/ward=5116/model=3/auth=07/mac=52-54-00-AA-D6-BE/ip=10-68-113-210/hostname=PC-AAD6BE" % pid
    # data_auth = {
    #     "user":"5613",
    #     "patno":"23097208",
    #     "ward":"5116",
    #     "model":"3",
    #     "auth":"07",
    #     "mac":"52-54-00-AA-D6-BE",
    #     "ip":"10-68-113-210",
    #     "hostname":"PC-AAD6BE"
    # }
    users = MySession()
    users.get(url_auth, headers=headers)
    # 获取检查列表
    url_post_report_list = "http://10.68.60.145:8001/Clinician/FunsCL/GetReport"

    post_data_report_list = {
        "searchflag":2,
        "dtBegin":start_time,
        "dtEnd":end_time,
        "deptcode":"5116",
        "patno":str(pid),
        "patid":None,
        "bedno":None,
        "patname":None,
        "wardcode":None,
        "barcode":None,
        "flagprint":2,
        "specimen":None,
        "idcard":None,
        "hoscode":None,
        "item":None,
        "inpid":None,
        "orino":None,
        "flagview":2,
        "jz":0,
        "wj":0,
        "self":0,
        "yg":0,
        "ny":0,
        "hj":0,

    }


    get_report_list = users.get(
        url_post_report_list,
        params=post_data_report_list,
        headers=headers
        )
    if get_report_list.status_code != 200:
        raise Exception('网络访问失败')
    get_report_list = get_report_list.json().get("datas")
    # 根据每条reportid获取检验详情
    url_report_details = "http://10.68.60.145:8001/Clinician/FunsCL/GetReportDetails"

    hyd = {}
    
    if get_report_list:
        for each in get_report_list:
            reportid = each.get('reportid')
            rptunitid = each.get('rptunitid')
            
            rechkdt = each.get('rechk_dt')
            patno = each.get('pat_no')
            patname = each.get('pat_name')
            req_reason = each.get('req_reason')
            # req_reason = each.get('req_reason')
            if reportid_required:
                reportid_required = str(reportid_required)
                if reportid != reportid_required:
                    continue

            post_data_report_details = {
                "reportid":reportid,
                "rptunitid":rptunitid,
                "rechkdt":rechkdt,
                "patno":patno,
                "patname":patname,
                "mode":1,
                "hisflag":None,
            }
            get_report_details = users.post(
                url_report_details,
                data=post_data_report_details,
                headers=headers
            ).json()
            for result in get_report_details.get('results'):
                
                rpt_itemname = result.get('rpt_itemname')
                
                hyd[rpt_itemname]={
                    "series": req_reason,
                    "high": "",
                    "low": ""
                }
        with open('lis_data_generated.json', 'w', encoding='utf-8') as f:
            json.dump(hyd, f, ensure_ascii=False)       


if __name__ == "__main__":

    pid = "23103635"
    reportid_required=None
    reportid_required="20230701-16-30"
    from datetime import datetime, timedelta
    end_time = datetime.strftime(datetime.today(), "%Y-%m-%d")
    start_time = datetime.strftime(datetime.today()+timedelta(days=-30), "%Y-%m-%d")

    lis_generator(pid, start_time, end_time, reportid_required=reportid_required)